package com.payment.web.util;

import cn.hutool.core.io.FileUtil;
//import com.cloud.job.config.Config;
import com.payment.web.model.domain.PdfDomain;
import lombok.extern.slf4j.Slf4j;
import org.jodconverter.DocumentConverter;
import org.jodconverter.document.DefaultDocumentFormatRegistry;
import org.jodconverter.office.OfficeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

@Slf4j
@Component
public class WordUtil {
    @Resource
    private DocumentConverter documentConverter;

    public String toPdf(PdfDomain req) {
        try {
            String wordPath = req.getWordPath();
            String contractDir = req.getContractDir();
            File file = new File(wordPath);
            InputStream in = new FileInputStream(file);
            //截取字符串把word的后缀改为pdf
            String fileName = FileUtil.getName(wordPath);
            String prefix = FileUtil.getPrefix(fileName);
            String pdfPath = contractDir + "/pdf/" + prefix + ".pdf";
            File pdf = new File(pdfPath);
            log.info("开始转换：");
            documentConverter.convert(in).as(DefaultDocumentFormatRegistry.DOCX).to(pdf).execute();
            //文件转换
            log.info("转换完成：" + pdfPath);
            return pdfPath;
        } catch (FileNotFoundException e) {
            log.info("FileNotFoundException：" + e.getMessage());
        } catch (OfficeException e) {
            log.info("OfficeException：" + e.getMessage());
        }
        return "";
    }


}
